<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>A Bytecode Virtual Machine &middot; Crafting Interpreters</title>

<!-- Tell mobile browsers we're optimized for them and they don't need to crop
     the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="style.css" />

<!-- Oh, God, Source Code Pro is so beautiful it makes me want to cry. -->
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400|Source+Sans+Pro:300,400,600' rel='stylesheet' type='text/css'>

<link rel="icon" type="image/png" href="image/favicon.png" />
<script src="jquery-3.4.1.min.js"></script>
<script src="script.js"></script>

<!-- Google analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-42804721-2', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body id="top">

<!-- <div class="scrim"></div> -->
<nav class="wide">
  <a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
  <div class="contents">
<h2><small>III</small>A Bytecode Virtual Machine</h2>

<ul>
    <li><a href="chunks-of-bytecode.html"><small>14</small>Chunks of Bytecode</a></li>
    <li><a href="a-virtual-machine.html"><small>15</small>A Virtual Machine</a></li>
    <li><a href="scanning-on-demand.html"><small>16</small>Scanning on Demand</a></li>
    <li><a href="compiling-expressions.html"><small>17</small>Compiling Expressions</a></li>
    <li><a href="types-of-values.html"><small>18</small>Types of Values</a></li>
    <li><a href="strings.html"><small>19</small>Strings</a></li>
    <li><a href="hash-tables.html"><small>20</small>Hash Tables</a></li>
    <li><a href="global-variables.html"><small>21</small>Global Variables</a></li>
    <li><a href="local-variables.html"><small>22</small>Local Variables</a></li>
    <li><a href="jumping-back-and-forth.html"><small>23</small>Jumping Back and Forth</a></li>
    <li><a href="calls-and-functions.html"><small>24</small>Calls and Functions</a></li>
    <li><a href="closures.html"><small>25</small>Closures</a></li>
    <li><a href="garbage-collection.html"><small>26</small>Garbage Collection</a></li>
    <li><a href="classes-and-instances.html"><small>27</small>Classes and Instances</a></li>
    <li><a href="methods-and-initializers.html"><small>28</small>Methods and Initializers</a></li>
    <li><a href="superclasses.html"><small>29</small>Superclasses</a></li>
    <li><a href="optimization.html"><small>30</small>Optimization</a></li>
</ul>


<div class="prev-next">
    <a href="inheritance.html" title="Inheritance" class="left">&larr;&nbsp;Previous</a>
    <a href="contents.html" title="Table of Contents">&uarr;&nbsp;Up</a>
    <a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="right">Next&nbsp;&rarr;</a>
</div>  </div>
</nav>

<nav class="narrow">
<a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
<a href="inheritance.html" title="Inheritance" class="prev">←</a>
<a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="next">→</a>
</nav>

<div class="page">
<div class="nav-wrapper">
<nav class="floating">
  <a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
  <div class="expandable">
<h2><small>III</small>A Bytecode Virtual Machine</h2>

<ul>
    <li><a href="chunks-of-bytecode.html"><small>14</small>Chunks of Bytecode</a></li>
    <li><a href="a-virtual-machine.html"><small>15</small>A Virtual Machine</a></li>
    <li><a href="scanning-on-demand.html"><small>16</small>Scanning on Demand</a></li>
    <li><a href="compiling-expressions.html"><small>17</small>Compiling Expressions</a></li>
    <li><a href="types-of-values.html"><small>18</small>Types of Values</a></li>
    <li><a href="strings.html"><small>19</small>Strings</a></li>
    <li><a href="hash-tables.html"><small>20</small>Hash Tables</a></li>
    <li><a href="global-variables.html"><small>21</small>Global Variables</a></li>
    <li><a href="local-variables.html"><small>22</small>Local Variables</a></li>
    <li><a href="jumping-back-and-forth.html"><small>23</small>Jumping Back and Forth</a></li>
    <li><a href="calls-and-functions.html"><small>24</small>Calls and Functions</a></li>
    <li><a href="closures.html"><small>25</small>Closures</a></li>
    <li><a href="garbage-collection.html"><small>26</small>Garbage Collection</a></li>
    <li><a href="classes-and-instances.html"><small>27</small>Classes and Instances</a></li>
    <li><a href="methods-and-initializers.html"><small>28</small>Methods and Initializers</a></li>
    <li><a href="superclasses.html"><small>29</small>Superclasses</a></li>
    <li><a href="optimization.html"><small>30</small>Optimization</a></li>
</ul>


<div class="prev-next">
    <a href="inheritance.html" title="Inheritance" class="left">&larr;&nbsp;Previous</a>
    <a href="contents.html" title="Table of Contents">&uarr;&nbsp;Up</a>
    <a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="right">Next&nbsp;&rarr;</a>
</div>  </div>
  <a id="expand-nav">≡</a>
</nav>
</div>

<article class="chapter">

  <div class="number">III</div>
  <h1 class="part">A Bytecode Virtual Machine</h1>

<p>Our Java interpreter, jlox, taught us many of the fundamentals of programming
languages, but we still have much to learn. First, if you run any interesting
Lox programs in jlox, you&rsquo;ll discover it&rsquo;s achingly slow. The style of
interpretation it uses<span class="em">&mdash;</span>walking the AST directly<span class="em">&mdash;</span>is good enough for <em>some</em>
real-world uses, but leaves a lot to be desired for a general-purpose scripting
language.</p>
<p>Also, we implicitly rely on runtime features of the JVM itself. We take for
granted that things like <code>instanceof</code> in Java work <em>somehow</em>. And we never for a
second worry about memory management because the JVM&rsquo;s garbage collector takes
care of it for us.</p>
<p>When we were focused on high-level concepts, it was fine to gloss over those.
But now that we know our way around an interpreter, it&rsquo;s time to dig down to
those lower layers and build our own virtual machine from scratch using nothing
more than the C standard library<span class="ellipse">&thinsp;.&thinsp;.&thinsp;.&nbsp;</span></p>

<footer>
<a href="chunks-of-bytecode.html" class="next">
  Next Chapter: &ldquo;Chunks of Bytecode&rdquo; &rarr;
</a>
Handcrafted by Robert Nystrom&ensp;&mdash;&ensp;<a href="https://github.com/munificent/craftinginterpreters/blob/master/LICENSE" target="_blank">&copy; 2015&hairsp;&ndash;&hairsp;2021</a>
</footer>
</article>

</div>
</body>
</html>
